﻿var companyId = '';
var ztId = '';
var kjnd = '';
var kjkm = [];
var aj = [];
var pjh = [];
var zph = [];
var wldx = [];
var xm = [];
var bm = [];

var kjkm_name = [];
var aj_name = [];
var pjh_name = [];
var zph_name = [];
var wldx_name = [];
var xm_name = [];
var bm_name = [];
var kmtitle = "";
var userdefault = {};
var params = {};
var dataList = [];
var mapList = [{id: "col_pzid", field: "pzid"}, {id: "col_pzrq", field: "pzrq"}, {id: "col_pzh", field: "pzh"},
    {id: "col_flxh", field: "flxh"}, {id: "col_zy", field: "zy"}, {id: "col_j", field: "j"}, {id: "col_d", field: "d"},
    {id: "col_yefxStr", field: "yefxStr"}, {id: "col_ye", field: "ye"}];

//创建一个表格控件
var tbl = new QTable();

var bootstrap = function ($, learun) {

    var pageWidth = 1024;

    var _companyId = request("companyId");
    var _ztId = request("ztId");
    var _kjnd = request("kjnd");
    var _kjkm = decodeURI(request("kjkm"), "UTF-8");
    var _aj = decodeURI(request("aj"), "UTF-8");
    var _pjh = decodeURI(request("pjh"), "UTF-8");
    var _zph = decodeURI(request("zph"), "UTF-8");
    var _wldx = decodeURI(request("wldx"), "UTF-8");
    var _xm = decodeURI(request("xm"), "UTF-8");
    var _bm = decodeURI(request("bm"), "UTF-8");
    var _pzrqStart = request("pzrqStart");
    var _pzrqEnd = request("pzrqEnd");

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            if(!!_companyId){
                userdefault.companyId = _companyId;
            }
            if(!!_ztId){
                userdefault.ztId = _ztId;
            }
            if(!!_kjnd){
                userdefault.kjnd = _kjnd;
            }

            //初始化打印插件
            hiprint.init();
            pageWidth = ($('.lr-layout-grid').width());
            $("#divDataList").height($(window).height() - 190);

            tbl.BindDom("divDataList"); //绑定到模板
            tbl.setMaplist(mapList);  //设置数据映射关系
            page.bind();
        },
        bind: function () {
            $('#multiple_condition_query').lrMultipleQuery(function (queryJson) {
                page.search(queryJson);
            }, 440, 800);
            $('#pzrqMonth').lrselect({
                type: 'default',
                // 展开最大高度
                maxHeight: 300,
                placeholder: '月份',
                data: [
                    {'id': '01', 'text': '01'},
                    {'id': '02', 'text': '02'},
                    {'id': '03', 'text': '03'},
                    {'id': '04', 'text': '04'},
                    {'id': '05', 'text': '05'},
                    {'id': '06', 'text': '06'},
                    {'id': '07', 'text': '07'},
                    {'id': '08', 'text': '08'},
                    {'id': '09', 'text': '09'},
                    {'id': '10', 'text': '10'},
                    {'id': '11', 'text': '11'},
                    {'id': '12', 'text': '12'}
                ],
                select: function (item) {
                    if (!item || item.id == '-1') {
                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-" + 12 + "-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);
                    } else {
                        var kjqj = item.id;
                        var beginDate = kjnd + "-" + kjqj + "-01";
                        var day = new Date(kjnd, kjqj, 0);
                        var endDate = kjnd + "-" + kjqj + "-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);
                    }
                }
            });
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;

                        $('#zt_select').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'zts/dropdownlist?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId},
                            select: function (ztItem) {
                                if (!ztItem || ztItem.value == '-1') {
                                    ztId = "";
                                } else {
                                    ztId = ztItem.value;
                                    $("#zt_title").html(ztItem.text);
                                    $('#kjnd_select').lrselectRefresh({
                                        // 访问数据接口地址
                                        url: config.base_server() + 'kjnds/dropdownlist?access_token=' + config.getToken().access_token,
                                        // 访问数据接口参数
                                        param: {companyId: companyId, ztId: ztItem.value},
                                    }).lrselectSet(userdefault.kjnd);
                                }
                            }
                        }).lrselectSet(userdefault.ztId);
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 帐套选择
            $('#zt_select').lrselect({
                type: 'default',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 300
            });
            // 会计年度选择
            $('#kjnd_select').lrselect({
                type: 'default',
                //placeholder: '请选择会计年度',
                // 展开最大高度
                maxHeight: 300,
                select: function (item) {
                    if (!item || item.value == '-1') {
                        kjnd = '';
                    } else {
                        kjnd = item.value;

                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-12-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);

                        $('#kjkmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'kjkms/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '科目', name: 'codeAndName', width: 225, align: 'left'},
                                {
                                    label: "余额方向", name: "yefx", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "借" : "贷";
                                    }
                                },
                                {
                                    label: "项目", name: "isXmHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "部门", name: "isBmHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "案件", name: "isAjHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "票据号", name: "isPjhHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "支票号", name: "isZphHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "往来", name: "isWlHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {label: '是否明细', name: 'isMx', width: 1, align: 'left', hidden: true},
                                {label: '编号', name: 'code', width: 120, align: 'left', hidden: true},
                                {label: '简称', name: 'simpleName', width: 225, align: 'left', hidden: true}
                            ],
                            select: function (values, texts) {
                                kjkm = values;
                                kjkm_name = texts;
                            }
                        });
                        $('#xmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'xms/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                                {label: '编号', name: 'code', width: 120, align: 'left'},
                            ],
                            select: function (values, texts) {
                                xm = values;
                                xm_name = texts;
                            }
                        });
                        $('#bmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'departments?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                                {label: '编号', name: 'code', width: 120, align: 'left'},
                            ],
                            select: function (values, texts) {
                                bm = values;
                                bm_name = texts;
                            }
                        });
                        $('#ajCodes').lrlayerselect({
                            dataValueId: 'name',
                            dataTextId: 'name',
                            enableInput: true,
                            reset: true,
                            url: config.base_server() + 'ajs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'name', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                            ],
                            select: function (values, texts) {
                                aj = values;
                                aj_name = texts;
                            }
                        });
                        $('#pjhCodes').lrlayerselect({
                            dataValueId: 'name',
                            dataTextId: 'name',
                            enableInput: true,
                            reset: true,
                            url: config.base_server() + 'pjhs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=1' + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'name', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                            ],
                            select: function (values, texts) {
                                pjh = values;
                                pjh_name = texts;
                            }
                        });
                        $('#zphCodes').lrlayerselect({
                            dataValueId: 'name',
                            dataTextId: 'name',
                            enableInput: true,
                            reset: true,
                            url: config.base_server() + 'pjhs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=2' + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'name', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                            ],
                            select: function (values, texts) {
                                zph = values;
                                zph_name = texts;
                            }
                        });
                        $('#wldxCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            enableInput: true,
                            reset: true,
                            url: config.base_server() + 'wldxs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                                {label: '编号', name: 'code', width: 120, align: 'left', hidden: true},
                            ],
                            select: function (values, texts) {
                                wldx = values;
                                wldx_name = texts;
                            }
                        });


                        if (!!_companyId) {
                            companyId = _companyId;
                            ztId = _ztId;
                            kjnd = _kjnd;
                            var param = {};
                            if (!!_kjkm) {
                                param.kjkmEnable = 1;
                                var arr = _kjkm.split(' ');
                                param.kjkmCodes = arr[0];
                                kjkm.push(arr[0]);
                                kjkm_name.push(_kjkm);
                            }
                            if (!!_aj) {
                                param.ajEnable = 1;
                                var arr = _aj.split(' ');
                                param.ajCodes = arr[0];
                                aj.push(arr[0]);
                                aj_name.push(_aj);
                                $("#ajCodes_input").val(arr[0])
                            }
                            if (!!_pjh) {
                                param.pjhEnable = 1;
                                var arr = _pjh.split(' ');
                                param.pjhCodes = arr[0];
                                pjh.push(arr[0]);
                                pjh_name.push(_pjh);
                                $("#pjhCodes_input").val(arr[0])
                            }
                            if (!!_zph) {
                                param.zphEnable = 1;
                                var arr = _zph.split(' ');
                                param.zphCodes = arr[0];
                                zph.push(arr[0]);
                                zph_name.push(_pjh);
                                $("#zphCodes_input").val(arr[0])
                            }
                            if (!!_wldx) {
                                param.wldxEnable = 1;
                                var arr = _wldx.split(' ');
                                param.wldxCodes = arr[0];
                                wldx.push(arr[0]);
                                wldx_name.push(_wldx);
                                $("#wldxCodes_input").val(arr[0])
                            }

                            if (!!_xm) {
                                param.xmEnable = 1;
                                var arr = _xm.split(' ');
                                param.xmCodes = arr[0];
                                xm.push(arr[0]);
                                xm_name.push(_xm);
                            }
                            if (!!_bm) {
                                param.bmEnable = 1;
                                var arr = _bm.split(' ');
                                param.bmCodes = arr[0];
                                bm.push(arr[0]);
                                bm_name.push(_bm);
                            }
                            if (!!_pzrqStart) {
                                $("#pzrqStart").val(_pzrqStart);
                            }
                            if (!!_pzrqEnd) {
                                $("#pzrqEnd").val(_pzrqEnd);
                            }
                            page.search(param);
                        } else {
                           // page.search({});
                        }
                    }
                }
            });

            // 查询
            $('#btn_Search').on('click', function () {
                page.search(params);
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            //打印预览
            $('#lr_printview').on('click', function () {
                var hiprintTemplate = null;
                var pzrqStart = $("#pzrqStart").val();
                var pzrqEnd = $("#pzrqEnd").val();
                var title = "";
                if ($.isArray(kjkm)) {
                    if (kjkm_name.length > 0) {
                        title += "科目：" + kjkm_name.join();
                    }
                }
                if ($.isArray(aj)) {
                    if (aj_name.length > 0) {
                        title += "  案件：" + aj_name.join();
                    }
                }
                if ($.isArray(pjh)) {
                    if (pjh_name.length > 0) {
                        title += "  票据号：" + pjh_name.join();
                    }
                }
                if ($.isArray(zph)) {
                    if (zph_name.length > 0) {
                        title += "  支票号：" + zph_name.join();
                    }
                }
                if ($.isArray(wldx)) {
                    if (wldx_name.length > 0) {
                        title += "  往来对象：" + wldx_name.join();
                    }
                }
                if ($.isArray(xm)) {
                    if (xm_name.length > 0) {
                        title += "  项目：" + xm_name.join();
                    }
                }
                if ($.isArray(bm)) {
                    if (bm_name.length > 0) {
                        title += "  部门：" + bm_name.join();
                    }
                }
                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=fzxmxz', function (res) {
                    if (res.resp_code == 200) {
                        hiprintTemplate = new hiprint.PrintTemplate({
                            template: JSON.parse(res.data.templete),
                            paginationContainer: '.hiprint-printPagination'
                        });

                        var item = {};
                        item.pzflList = dataList;
                        if (item.pzflList.length > 72) {
                            item.pzflList = item.pzflList.slice(0, 72);
                        }

                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        item.kjkmName = title;
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();
                        item.pzrq = pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10);
                        //hiprintTemplate.print(item);
                        hiprintTemplate.resetPage();
                        $('#myModal .modal-body .prevViewDiv').html(hiprintTemplate.getHtml(item));
                        $('#myModal').modal('show');
                    }
                });
                $('#A4_printByHtml').click(function () {
                    hiprintTemplate.printByHtml($('#myModal .modal-body .prevViewDiv'));
                })
            });

            //打印
            $('#lr_print').on('click', function () {
                var hiprintTemplate = null;
                var pzrqStart = $("#pzrqStart").val();
                var pzrqEnd = $("#pzrqEnd").val();
                var title = "";
                if ($.isArray(kjkm)) {
                    if (kjkm_name.length > 0) {
                        title += "科目：" + kjkm_name.join();
                    }
                }
                if ($.isArray(aj)) {
                    if (aj_name.length > 0) {
                        title += "  案件：" + aj_name.join();
                    }
                }
                if ($.isArray(pjh)) {
                    if (pjh_name.length > 0) {
                        title += "  票据号：" + pjh_name.join();
                    }
                }
                if ($.isArray(zph)) {
                    if (zph_name.length > 0) {
                        title += "  支票号：" + zph_name.join();
                    }
                }
                if ($.isArray(wldx)) {
                    if (wldx_name.length > 0) {
                        title += "  往来对象：" + wldx_name.join();
                    }
                }
                if ($.isArray(xm)) {
                    if (xm_name.length > 0) {
                        title += "  项目：" + xm_name.join();
                    }
                }
                if ($.isArray(bm)) {
                    if (bm_name.length > 0) {
                        title += "  部门：" + bm_name.join();
                    }
                }
                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=fzxmxz', function (res) {
                    if (res.resp_code == 200) {
                        hiprintTemplate = new hiprint.PrintTemplate({
                            template: JSON.parse(res.data.templete),
                            paginationContainer: '.hiprint-printPagination'
                        });

                        var item = {};
                        item.pzflList = dataList;
                        // for (var i = 0; i < item.pzflList.length; i++) {
                        //     item.pzflList[i].j = $.formatMoney(item.pzflList[i].j, 2);
                        //     item.pzflList[i].d = $.formatMoney(item.pzflList[i].d, 2);
                        //     item.pzflList[i].ye = $.formatMoney(item.pzflList[i].ye, 2);
                        // }
                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        item.kjkmName = title;
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();
                        item.pzrq = pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10);
                        hiprintTemplate.resetPage();
                        hiprintTemplate.print2(item);
                    }
                });
            });

            //导出Excel
            $("#lr_export_excel").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;
                params.kmName = kmtitle;
                learun.download({
                    url: config.base_server() + 'reports/fzxmxz/excel',
                    param: params
                });
            });
            //导出pdf
            $("#lr_export_pdf").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;
                params.kmName = kmtitle;
                learun.download({
                    url: config.base_server() + 'reports/fzxmxz/pdf',
                    param: params
                });
            });
        },

        search: function (param) {
            param = param || {};
            var pzrqStart = $("#pzrqStart").val();
            var pzrqEnd = $("#pzrqEnd").val();
            param.companyId = companyId;
            param.ztId = ztId;
            param.kjnd = kjnd;
            param.pzrqStart = pzrqStart;
            param.pzrqEnd = pzrqEnd;

            var formData = $('.lr-query-formcontent').lrGetFormData();

            if (!!_companyId) {

            } else {
                param.kjkmEnable = formData.kjkmEnable;
                param.ajEnable = formData.ajEnable;
                param.pjhEnable = formData.pjhEnable;
                param.zphEnable = formData.zphEnable;
                param.wldxEnable = formData.wldxEnable;
                param.xmEnable = formData.xmEnable;
                param.bmEnable = formData.bmEnable;
            }
            var title = "";
            if ($.isArray(kjkm)) {
                param.kjkmCodes = kjkm.join();
                if (kjkm_name.length > 0) {
                    title += "科目：" + kjkm_name.join();
                }
            }
            if ($.isArray(aj)) {
                param.ajCodes = aj.join();
                if (aj_name.length > 0) {
                    title += "  案件：" + aj_name.join();
                }
            }
            if ($.isArray(pjh)) {
                param.pjhCodes = pjh.join();
                if (pjh_name.length > 0) {
                    title += "  票据号：" + pjh_name.join();
                }
            }
            if ($.isArray(zph)) {
                param.zphCodes = zph.join();
                if (zph_name.length > 0) {
                    title += "  支票号：" + zph_name.join();
                }
            }
            if ($.isArray(wldx)) {
                param.wldxCodes = wldx.join();
                if (wldx_name.length > 0) {
                    title += "  往来对象：" + wldx_name.join();
                }
            }
            if ($.isArray(xm)) {
                param.xmCodes = xm.join();
                if (xm_name.length > 0) {
                    title += "  项目：" + xm_name.join();
                }
            }
            if ($.isArray(bm)) {
                param.bmCodes = bm.join();
                if (bm_name.length > 0) {
                    title += "  部门：" + bm_name.join();
                }
            }

            if (formData.kjkmEnable == 0 && formData.ajEnable == 0 && formData.pjhEnable == 0 && formData.zphEnable == 0 && formData.wldxEnable == 0 && formData.bmEnable == 0 && formData.xmEnable == 0) {
                learun.alert.error("至少选择一个辅助项");
                return;
            }
            if ($("#ajCodes_input").val().trim() == "") {
                param.ajCodes = "";
            }
            if ($("#pjhCodes_input").val().trim() == "") {
                param.pjhCodes = "";
            }
            if ($("#zphCodes_input").val().trim() == "") {
                param.zphCodes = "";
            }
            if ($("#wldxCodes_input").val().trim() == "") {
                param.wldxCodes = "";
            }
            params = param;
            learun.loading(true);
            learun.httpPost2(config.base_server() + 'reports/fzxmxz?access_token=' + config.getToken().access_token, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    dataList = res.data;
                    for (var i = 0; i < dataList.length; i++) {
                        dataList[i].j = $.formatMoney(dataList[i].j, 2);
                        dataList[i].d = $.formatMoney(dataList[i].d, 2);
                        dataList[i].ye = $.formatMoney(dataList[i].ye, 2);
                    }
                    tbl.BindData(dataList);
                } else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
            });
            kmtitle = title;
            $("#pzrq_title").html(pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10));
            $("#kjkm_title").html(kmtitle);
        }
    };
    page.init();
}

//查看凭证
function ViewData(row) {
    var data = row.data;
    var keyValue = data.pzid;
    var flxh = data.flxh;
    if (keyValue) {
        top.learun.frameTab.open({
            id: 'pz_view' + data.pzh,
            css: 'fa fa-file-text-o',
            name: '查看凭证' + data.pzh,
            path: config.webportal_server + 'pages/finance/pz/view.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&keyValue=' + keyValue + '&flxh=' + flxh + '&access_token=' + config.getToken().access_token
        });
    }
}
